package com.wiserun.develop.processrecord.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wiserun.develop.apply.mapper.ApplyMapper;
import com.wiserun.develop.apply.model.Apply;
import com.wiserun.develop.apply.model.ApplyDTO;
import com.wiserun.develop.apply.model.ApplyListDTO;
import com.wiserun.develop.apply.service.ApplyService;
import com.wiserun.develop.message.model.MessageDTO;
import com.wiserun.develop.message.service.MessageService;
import com.wiserun.develop.messageconfig.mapper.MessageConfigMapper;
import com.wiserun.develop.messageconfig.model.MessageConfig;
import com.wiserun.develop.processconfig.model.ProcessConfig;
import com.wiserun.develop.processconfig.model.ProcessConfigDTO;
import com.wiserun.develop.processconfig.service.ProcessConfigService;
import com.wiserun.develop.processrecord.mapper.ProcessRecordMapper;
import com.wiserun.develop.processrecord.model.ProcessRecord;
import com.wiserun.develop.processrecord.model.ProcessRecordDTO;
import com.wiserun.develop.processrecord.model.ProcessRecordListDTO;

@Service
public class ProcessRecordService {
	
	@Autowired
    private Mapper mapperTrans;
	@Autowired
	private ProcessRecordMapper mapper;
	@Autowired
	private ApplyMapper amapper;
	@Autowired
	private MessageConfigMapper mcmapper;
	@Autowired
	private HttpSession session;
	@Autowired
	private ApplyService aservice;
	@Autowired
	private ProcessConfigService pcservice;
	@Autowired
	private MessageService mservice;
	/**
	 *根据编号查询对应的一条详细数据
	 *创建日期 2019-8-8
	 *liangbenjian 
	 */
	public ProcessRecord getOne(ProcessRecordDTO container) {
		return mapper.queryOne1(container.getSPR_Code());
	}
	
	public ProcessRecord getOne2(ProcessRecordDTO container) {
		return mapper.queryOne2(container.getSPR_ModelCode());
	}
	
	
	/**
	 *新增一条信息
	 *创建日期 2019-8-8
	 *liangbenjian 
	 */
	public Integer add(ProcessRecordDTO container) {
		ProcessRecord processrecord=new ProcessRecord();
		Date now = new Date(); 
		String username=(String) session.getAttribute("user");//拿到session里存储的登录人名字
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//创建日期的格式
		String hehe = dateFormat.format(now);
		UUID uuid = UUID.randomUUID();
        String strID = uuid.toString();	
        processrecord.setSPR_Code(strID);
        processrecord.setSPR_CreateTime(hehe);
        processrecord.setSPR_CreateUser(username);
        processrecord.setSPR_State("1");
        processrecord.setSPR_Model(container.getSPR_Model());
        processrecord.setSPR_ModelCode(container.getSPR_ModelCode());
        processrecord.setSPR_Action(container.getSPR_Action());
        processrecord.setSPR_Result(container.getSPR_Result());
        processrecord.setSPR_Description(container.getSPR_Description());
        processrecord.setSPR_Batch(container.getSPR_Batch());
        processrecord.setSPR_ConfigNo(container.getSPR_ConfigNo());
        return mapper.add(processrecord);
        
    }
	/**
	 *根据编号查询对应的一条详细数据然后更新
	 *创建日期 2019-8-8
	 *liangbenjian 
	 */
	public Integer update(ProcessRecordDTO container) {
		ProcessRecord processrecord=mapper.queryOne1(container.getSPR_Code());
		processrecord.setSPR_State(container.getSPR_State());
		processrecord.setSPR_Model(container.getSPR_Model());
		processrecord.setSPR_ModelCode(container.getSPR_ModelCode());
		processrecord.setSPR_Action(container.getSPR_Action());
		processrecord.setSPR_Result(container.getSPR_Result());
		processrecord.setSPR_Description(container.getSPR_Description());
		processrecord.setSPR_Batch(container.getSPR_Batch());
		processrecord.setSPR_ConfigNo(container.getSPR_ConfigNo());
        return mapper.update(processrecord);
        
    }
	
	
	/**
	 *可根据是否有条件查询所有流程数据
	 *创建日期 2019-8-9
	 *liangbenjian 
	 */
	public List<ProcessRecordListDTO> getPageList(ProcessRecordDTO container){
		ProcessRecord processRecord= mapperTrans.map(container,ProcessRecord.class);	//封装数据到对象里
		Map<String, Object> map = new HashMap<>();

			map.put("SPR_Code", processRecord.getSPR_Code());         //map中的key值与映射文件中的select语句#{}占位符中的值需要一一对应
			map.put("SPR_CreateUser",processRecord.getSPR_CreateUser());	
			map.put("SPR_ModelCode",processRecord.getSPR_ModelCode());		
/*	       System.out.println(map.get("model"));
	       System.out.println(map.get("LoginUser"));*/
	        List<ProcessRecord> ProcessRecordList= mapper.queryPageList(map);            //将条件全部给方法得到查询出的数据
	        List<ProcessRecordListDTO> list = new ArrayList<>();                     //放入list
	        for (ProcessRecord processrecord : ProcessRecordList) {
	        	ProcessRecordListDTO dto = mapperTrans.map(processrecord, ProcessRecordListDTO.class);
	            list.add(dto);
	        }
			return list;	        
	}
	
	/**
	 *保存审批结果
	 *创建日期 2019-8-12
	 *liangbenjian 
	 */
	public Integer save(ProcessRecordDTO container) {
		//创建流程记录表新对象
		ProcessRecord processRecord= new ProcessRecord();
		Date now = new Date(); 
		String username=(String) session.getAttribute("user");//拿到session里存储的登录人名字
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//创建日期的格式
		String hehe = dateFormat.format(now);
		UUID uuid = UUID.randomUUID();
        String strID = uuid.toString(); 	
        session.setAttribute("SPR_ModelCode",container.getSPR_ModelCode());
        processRecord.setSPR_Code(strID);//设置流程编号UUID
        processRecord.setSPR_CreateTime(hehe);//创建时间
        processRecord.setSPR_CreateUser(username);//创建人
        processRecord.setSPR_State("1");//状态
        processRecord.setSPR_Model("M1.1.1");//审批的流程模块只有这个
        processRecord.setSPR_ModelCode(container.getSPR_ModelCode());//流程主键，前台传过来   
        processRecord.setSPR_Result(container.getSPR_Result());//操作结果，前台传输
/*        if(container.getSPR_Result().equalsIgnoreCase("Y")) {
        	processRecord.setSPR_Result("通过");//操作结果，前台传输
        }
        if(container.getSPR_Result().equalsIgnoreCase("N")) {
        	processRecord.setSPR_Result("驳回");//操作结果，前台传输
        }*/
        processRecord.setSPR_Description(container.getSPR_Description());//描述，前台传输
        //通过流程主键查到资源申请主表的流程批次并设置
        ApplyDTO a=new ApplyDTO();
        System.out.println("-------------------------------"+container.getSPR_ModelCode());
        a.setRA_Code(container.getSPR_ModelCode());
        Apply apply=aservice.getOne(a);
        String state1=apply.getRA_State();//拿到状态判断得到操作
        if(state1.equalsIgnoreCase("A0")){
            processRecord.setSPR_Action("一审");//操作
        }else if(state1.equalsIgnoreCase("A1")) {
        	 processRecord.setSPR_Action("二审");//操作
        }else if(state1.equalsIgnoreCase("A2")) {
       	 processRecord.setSPR_Action("三审");//操作
        }else if(state1.equalsIgnoreCase("A3")) {
      	 processRecord.setSPR_Action("四审");//操作
        }
        processRecord.setSPR_Batch(apply.getRA_Batch());
        //通过主表的状态和申请类型查到流程配置编号并设置---------
        ProcessConfigDTO p=new ProcessConfigDTO();
        p.setSPC_Step(apply.getRA_State());
        /*p.setSPC_Version(apply.getRA_ApplyType());*/
        //版本号暂时给一个固定版本号
        p.setSPC_Version("1");
        ProcessConfig pc= pcservice.getOne2(p);
        processRecord.setSPR_ConfigNo(pc.getSPC_Code());
        //判断操作结果
        String r=container.getSPR_Result();
        if(r.equals("N")) {
        	//说明审核被驳回,更改主表状态B+数字
        	String state=apply.getRA_State();
        	String s2=state.substring(1, state.length()); 	//数字
        	int ss=Integer.parseInt(s2);
        	ss=ss+1;//数字+1
        	state="B"+ss;//新的状态
        	apply.setRA_State(state);
        	System.out.println("---------------更新资源主表编号为"+apply.getRA_Code()+"状态为"+state+"的资源主表状态---------------");
        	amapper.update(apply);
        	//生成通知消息
        	MessageDTO m=new MessageDTO();
        	m.setApplyCode(apply.getRA_Code());
        	m.setSM_Model("M1.1.1");
        	m.setSM_Operate(processRecord.getSPR_Action()+"驳回");
        	System.out.println("---------------生成状态为"+state+"的通知消息---------------");
        	mservice.add(m);
        }else if(r.equals("Y")) {
        	//审核通过的话状态变为下个状态
        	String state=apply.getRA_State();
        	String s2=state.substring(1, state.length()); 	//数字
        	int ss=Integer.parseInt(s2);
        	ss=ss+1;//数字+1
        	state="A"+ss;//新的状态
        	apply.setRA_State(state);
        	System.out.println("---------------更新资源主表编号为"+apply.getRA_Code()+"状态为"+state+"的资源主表状态---------------");
        	amapper.update(apply);
        	MessageDTO m=new MessageDTO();
        	m.setApplyCode(apply.getRA_Code());
        	m.setSM_Model("M1.1.1");
        	m.setSM_Operate(processRecord.getSPR_Action()+"通过");
        	System.out.println("---------------生成状态为"+state+"的通知消息---------------");
        	mservice.add(m);
        	
        }
        System.out.println("---------------生成编号为为"+strID+"的流程记录---------------");
        return mapper.add(processRecord);
             
    }

}
